package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);

        return R.success("添加成功...");

    }

    @GetMapping("/page")
    public R<Page> getpage(int page,int pageSize){

        Page<Category> page2 = new Page(page,pageSize);

        categoryService.page(page2);

        return R.success(page2);
    }

    @DeleteMapping
    public R<String> delete(Long id){

        categoryService.remove(id);

        return R.success("删除完成...");

    }

    @PutMapping
    public R<String> update(@RequestBody Category category){

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getId,category.getId());
        categoryService.update(category,wrapper);
        return R.success("修改成功...");

    }

    @GetMapping("/list")
    public R<List<Category>> list(Integer type){

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type!=null,Category::getType,type);
        List<Category> list = categoryService.list(wrapper);

        return R.success(list);

    }

}



